<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            padding: 0;
            margin: 0;
        }

        html,
        body,
        .room {
            height: 100%;
            width: 100%;
        }

        .room {
            display: flex;
        }

        .left {
            width: 300px;
            border-right: 0.5px solid #f5f5f5;
            background: #333;
        }

        .right {
            background: #1c1c1c;
            flex: 1;
            display: flex;
            flex-direction: column;
        }

        .header {
            background: #8d0eb0;
            color: white;
            padding: 10px;
            box-sizing: border-box;
            font-size: 20px;
        }

        .main {
            flex: 1;
            padding: 10px;
            box-sizing: border-box;
            font-size: 20px;
            overflow: auto;
        }

        .main-chat {
            color: green;
        }

        .footer {
            min-height: 200px;
            border-top: 1px solid green;
        }

        .footer .ipt {
            width: 100%;
            height: 100%;
            color: green;
            outline: none;
            font-size: 20px;
            padding: 10px;
            box-sizing: border-box;
        }

        .groupList {
            height: 100%;
            overflow: auto;
        }

        .groupList-items {
            height: 50px;
            width: 100%;
            background: #131313;
            display: flex;
            align-items: center;
            justify-content: center;
            color: white;
        }
    </style>
</head>
<div class="room">
    <div class="left">
        <div class="groupList">

        </div>
    </div>
    <div class="right">
        <header class="header">聊天室</header>
        <main class="main">

        </main>
        <footer class="footer">
            <div class="ipt" contenteditable></div>
        </footer>
    </div>
</div>

<body>
    <script type="module">
        const name = prompt("请输入你的昵称");
        const room = prompt("请输入聊天室名称");
        const group = document.querySelector(".groupList");//左侧列表
        const main = document.querySelector(".main")//消息区域
        const ipt = document.querySelector("footer .ipt") //输入框

        const addChat = (msg)=>{
            const item = document.createElement("div");
            item.className = "main-chat";
            item.innerHTML = `${msg.name}：${msg.message}`;
            main.appendChild(item)
        }
        import { io } from "https://cdn.socket.io/4.7.4/socket.io.esm.min.js";
        const socket = io("ws://192.168.137.11:3000");

        socket.on("connect",()=>{
            // 1.加入房间
            socket.emit("join",{name,room})
            socket.on("groupMap",(data)=>{

                group.innerHTML = ``
                Object.keys(data).forEach(key=>{
                    const item = document.createElement("div");
                    item.className = "groupList-items";
                    item.innerHTML = `房间号：${key} 房间人数：${data[key].length}`
                    group.appendChild(item)
                })
            })
           
            // 2.监听回车发送消息
            ipt.addEventListener("keydown",(e)=>{
                console.log("回车");
                if(e.keyCode === 13){
                    const message = ipt.innerText.trim();
                    socket.emit("message",{name,message,room})
                    ipt.innerText = ""
                }
            })
            socket.on("message",(data)=>{
                addChat(data)
            })
        })
           
           </script>
</body>

</html>